package com.jade.store.crm.util;

import com.alibaba.fastjson.JSON;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.customer.CustomerException;
import com.jade.zsy.crm.entity.CrmInfo;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/5/1
 * Time: 7:35
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
public class IposUtil {
    public static int insertIposCustomer(CrmInfo crmInfo) throws CustomerException {
        String url="";
        if(crmInfo.getOwnerId().equals("37")){
            url="http://app.zsydian.com:10504/bl001/customer/add.do";
        }

        else if(crmInfo.getOwnerId().equals("78"))
            url="http://app.zsydian.com:10503/gc/customer/add.do";
        else return 200;
        String postDataXML = JSON.toJSONString(crmInfo);
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            byte[] requestStringBytes = postDataXML.getBytes("UTF-8");

            conn.setRequestProperty("Content-length", "" + requestStringBytes.length);
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
            conn.setRequestProperty("Charset", "UTF-8");

            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(requestStringBytes);
            outputStream.flush();
            outputStream.close();

            //定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return 200;
    }
    public static int updateIposCustomer(CrmInfo crmInfo) throws CustomerException {
        String url="";
        if(crmInfo.getOwnerId().equals("37"))
            url="http://app.zsydian.com:10504/bl001/customer/update.do";
        else if(crmInfo.getOwnerId().equals("78"))
            url="http://app.zsydian.com:10503/gc/customer/update.do";
        else return 200;
        String postDataXML = JSON.toJSONString(crmInfo);
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            byte[] requestStringBytes = postDataXML.getBytes("UTF-8");

            conn.setRequestProperty("Content-length", "" + requestStringBytes.length);
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
            conn.setRequestProperty("Charset", "UTF-8");

            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(requestStringBytes);
            outputStream.flush();
            outputStream.close();

            //定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return 200;
    }
    public static int updateIposCustomerChare(CrmInfo crmInfo) throws CustomerException {
        String url="";
         if(crmInfo.getOwnerId().equals("37"))
            url="http://app.zsydian.com:10504/bl001/customer/cz.do";
        else if(crmInfo.getOwnerId().equals("78"))
            url="http://app.zsydian.com:10503/gc/customer/cz.do";
        else return 200;
        String postDataXML = JSON.toJSONString(crmInfo);
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            byte[] requestStringBytes = postDataXML.getBytes("UTF-8");

            conn.setRequestProperty("Content-length", "" + requestStringBytes.length);
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
            conn.setRequestProperty("Charset", "UTF-8");

            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(requestStringBytes);
            outputStream.flush();
            outputStream.close();

            //定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return 200;
    }
}
